﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Windows.Forms;

namespace NotePadPlayer
{
    public partial class Form1 : Form
    {
        private string directory = string.Empty;
        private string fileNameHead = string.Empty;
        private int fileNameStart;
        private int fileNameEnd;
        private bool isPlaying = false;
        private bool isStop = true;
        private int fps = 1;
        private delegate void handleVoid();

        private bool IsPlaying
        {
            get { return isPlaying; }
            set
            {
                btnStop.Enabled = true;
                if (value)
                {
                    btnPlay.Text = "暂停";
                }
                else
                {
                    btnPlay.Text = "播放";
                }
                isPlaying = value;
            }
        }

        private bool IsStop
        {
            get { return isStop; }
            set
            {
                if (!value)
                {
                    btnStop.Enabled = true;
                }
                else
                {
                    btnPlay.Text = "播放";
                }
                isStop = value;
            }
        }

        bool[] validingGroup=new bool[4];
        public Form1()
        {
            InitializeComponent();
        }

        private void btnPlay_Click(object sender, EventArgs e)
        {
            if (!ValidatingAll())
                return;

            IsStop = false;
            IsPlaying = !IsPlaying;

            if (!backgroundWorker1.IsBusy)
                backgroundWorker1.RunWorkerAsync();

        }

        private bool ValidatingAll()
        {
            txtDirectory_Validating(this, null);
            txtFileNameStart_Validating(this, null);
            txtFileNameEnd_Validating(this, null);
            txtFileNameHead_Validating(this, null);
            txtFps_Validating(this, null);
            return validingGroup[0] && validingGroup[1] && validingGroup[2] && validingGroup[3];
        }

        private void txtDirectory_Validating(object sender, CancelEventArgs e)
        {
            if (Directory.Exists(txtDirectory.Text))
            {
                directory = txtDirectory.Text;
                validingGroup[0] = true;
            }
            else
            {
                MessageBox.Show("请输入正确路径");
                validingGroup[0] = false;
            }
        }

        private void txtFileNameStart_Validating(object sender, CancelEventArgs e)
        {
            if (txtFileNameStart.Text!="")
            {
                fileNameStart = Convert.ToInt32(txtFileNameStart.Text);
                validingGroup[1] = true;
            }
            else
            {
                MessageBox.Show("请输入数字开始范围");
                validingGroup[1] = false;
            }
        }

        private void txtFileNameEnd_Validating(object sender, CancelEventArgs e)
        {
            if (txtFileNameEnd.Text != "")
            {
                fileNameEnd = Convert.ToInt32(txtFileNameEnd.Text);
                validingGroup[2] = true;
            }
            else
            {
                MessageBox.Show("请输入数字结束范围");
                validingGroup[2] = false;
            }
        }

        private void txtFileNameHead_Validating(object sender, CancelEventArgs e)
        {
            fileNameHead = txtFileNameHead.Text;
        }

        private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
        {
            for (int i = fileNameStart; i <= fileNameEnd; i++)
            {
                if (IsStop)
                    return;

                while (!IsPlaying)
                {
                    if (IsStop)
                        return;
                    Thread.Sleep(100);
                }
                string path;
                if (directory.Substring(directory.Length - 2) == "\\")
                {
                    path = directory + fileNameHead + i + ".txt";
                    if (!File.Exists(path))
                    {
                        MessageBox.Show(path + " 文件不存在！");
                        IsPlaying = false;
                        return;
                    }
                }
                else
                {
                    path = directory + "\\" + fileNameHead + i + ".txt";
                    if (!File.Exists(path))
                    {
                        MessageBox.Show(path + " 文件不存在！");
                        IsPlaying = false;
                        return;
                    }
                }

                this.Invoke(new handleVoid(delegate
                {
                    {
                        lblFileName.Text = fileNameHead + i;
                    }
                })

            );

                try
                {
                    FileStream fileStream = new FileStream(path, FileMode.Open);
                    StreamReader streamReader = new StreamReader(fileStream);
                    string text = streamReader.ReadToEnd();
                    fileStream.Flush();
                    streamReader.Close();
                    fileStream.Close();
                    this.Invoke(new handleVoid(delegate
                    {
                        {
                            richTextBox1.Text = text;
                        }
                    })

);
                   
                }
                catch (Exception)
                {
                    MessageBox.Show(path + " 加载失败！");
                    return;
                }
                Thread.Sleep((int) (1000f / (float)fps));
            }
        }

        private void txtFps_Validating(object sender, CancelEventArgs e)
        {
            if (txtFps.Text != "")
            {
                fps = Convert.ToInt32(txtFps.Text);
                if (fps<=0)
                {
                    MessageBox.Show("请输入正确帧数");
                }
                validingGroup[3] = true;
            }
            else
            {
                MessageBox.Show("请输入正确帧数");
                validingGroup[3] = false;
            }
        }

        private void btnStop_Click(object sender, EventArgs e)
        {
            IsPlaying = false;
            IsStop = true;
            richTextBox1.Text = "";
            btnStop.Enabled = false;
        }


    }
}
